<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>

<body>
    <div id="app">
        {{ num }} <button @click="num++">+</button>
        <q-header :a="num" title="购物车"></q-header>
    </div>
</body>
<script>
    const { ref, createApp, computed } = Vue;
    const app = createApp({
        setup() {
            const num = ref(1);
            return {
                num
            }
        }
    })

    app.component('q-header', {
        props: {
            a: {
                type: [String, Number],
                required: false,
                validator(val) {
                    if (val <= 10) {
                        return true;
                    } else {
                        throw new Error('校验错误');
                        return false;
                    }
                }
            },
            title: String
        },
        setup(props) {
            let title = props.title;
            let a = computed(function () {
                return props.a * 2;
            });
            return {
                title,
                a
            }
        },
        template: `
            <div class="header">
                <h1>{{ title }}</h1>
                <h2>{{ a }}</h2>
            </div>
        `
    })

    app.mount('#app')
</script>

</html>